﻿using WeShare.WeiXin.Common;
using WeShare.WeiXin.Mp.MsgEntity;
using System.Xml.Linq;

#if NETSTANDARD2_0
using Microsoft.AspNetCore.Http;
#endif
#if NET40 || NET45
using System.Web;
#endif
#if !NET40
using System.Threading.Tasks;
#endif
namespace WeShare.WeiXin.Mp
{
    /// <summary>
    /// 回调服务类
    /// </summary>
    public static class CallBackService
    {
        public static string Register<T>(this HttpContext context, EnterParamConfig config) where T : BaseMsgHandler, new()
        {
            if (context.ValidUrl(config.token))
            {
                if (!context.Request.IsGet())
                {
                    var xml = context.GetRequestData();
                    var encrypt_type = context.Request.QueryValue("encrypt_type");
                    if (encrypt_type == "aes")
                    {
                        config.IsAes = true;
                        var ret = new WXBizMsgCrypt(config.token, config.EncodingAESKey, config.appid);
                        var timestamp = context.Request.QueryValue("timestamp");
                        var nonce = context.Request.QueryValue("nonce");
                        var msg_signature = context.Request.QueryValue("msg_signature");
                        string postStr = null;
                        if (ret.DecryptMsg(msg_signature, timestamp, nonce, xml, ref postStr) != 0)
                        {
                            return "解密失败，请检查参数配置";
                        }
                        xml = postStr;
                    }
                    else
                    {
                        config.IsAes = false;
                    }
                    return MessageFactory.GetInstance<T>().GetResponse(XElement.Parse(xml), config);
                }
                else
                {
                    return context.Request.QueryValue("echostr");
                }
            }
            else
            {
                return "签名验证失败";
            }
        }

#if !NET40
        public static async Task<string> RegisterAsync<T>(this HttpContext context, EnterParamConfig config) where T : BaseMsgHandler, new()
        {
            return await Task.Run(async () =>
            {
                if (context.ValidUrl(config.token))
                {
                    if (!context.Request.IsGet())
                    {
                        var xml = context.GetRequestData();
                        var encrypt_type = context.Request.QueryValue("encrypt_type");
                        if (encrypt_type == "aes")
                        {
                            config.IsAes = true;
                            var ret = new WXBizMsgCrypt(config.token, config.EncodingAESKey, config.appid);
                            var timestamp = context.Request.QueryValue("timestamp");
                            var nonce = context.Request.QueryValue("nonce");
                            var msg_signature = context.Request.QueryValue("msg_signature");
                            string postStr = null;
                            if (ret.DecryptMsg(msg_signature, timestamp, nonce, xml, ref postStr) != 0)
                            {
                                return "解密失败，请检查参数配置";
                            }
                            xml = postStr;
                        }
                        else
                        {
                            config.IsAes = false;
                        }
                        return await MessageFactory.GetInstance<T>().GetResponseAsync(XElement.Parse(xml), config);
                    }
                    else
                    {
                        return context.Request.QueryValue("echostr");
                    }
                }
                else
                {
                    return "签名验证失败";
                }
            });

        }
#endif

    }
}
